def f(n):
    for i in range(2,int(n**0.5)+1):
        if n%i==0: return False
    return True

b=[]
x=int(input())
if len(str(x))!=4:
    print(0)
else:
    x1=x//1000
    x2=x//100%10
    x3=x%100//100
    x4=x%10
    x5=x//100
    x6=x%1000//10
    x7=x%100
    x8=x//10
    x9=x%1000
    a=[x1,x2,x3,x4,x5,x6,x7,x8,x9,x]
    for m in a:
        if f(m):
            b.append(m)
    if len(b)>0:
        print(max(b))
    else:
        print(0)